package adm.hikingapp;

import java.util.ArrayList;

import Dominio.Ruta;
import adm.hikingapp.ClaseBase;
import adm.hikingapp.R;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * Clase utilizada para mostrar una lista de todas las rutas
 * @author Mario
 * @author Jose
 */
public class ListActivity extends ClaseBase {
	private ListView lv;
	private MyListAdapter adapter;
	private ArrayList<String> list;
	private ArrayList<Ruta> rutas;
	private LayoutInflater mInflater;
	
	@SuppressWarnings("unchecked")
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        list = new ArrayList<String>();
        rutas = new ArrayList<Ruta>();
        
        Bundle bundle = getIntent().getExtras();
		if(bundle != null){
			rutas = (ArrayList<Ruta>) bundle.getSerializable("rutas");
		}
                
        try {
        	for (int i=0; i<rutas.size(); i++) {
        		list.add(rutas.get(i).getNombre());
        	}
        } catch (Exception e) {}
        
        lv = (ListView) findViewById(R.id.list);
        mInflater = LayoutInflater.from(this);
        adapter = new MyListAdapter(this);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View v, int pos, long arg3) {
				SharedPreferences preferencias = getSharedPreferences("preferencias", Context.MODE_PRIVATE);
				Editor editor = preferencias.edit();
				editor.putInt("idRuta", rutas.get(pos).getId());
				//editor.putString("nick", rutas.get(pos).getUsuario());
				editor.commit();
				Intent intent = new Intent(ListActivity.this, InformacionRuta.class);
//				intent.putExtra("id_Ruta", rutas.get(pos).getId());
//				intent.putExtra("nick", rutas.get(pos).getUsuario());
				startActivity(intent);
			}
		});
	}
	
	
	
	/**
	 * @author Mario
	 * @author Jose
	 * @see ArrayAdapter
	 */
	public class MyListAdapter extends ArrayAdapter<String> {
		public MyListAdapter(Context context){
			super(context, R.layout.row_list_icon, list);
		}
				
		@Override
		public View getView(int position, View convertView, ViewGroup parent){
			final ViewHolder holder;
			final int pos = position;
			
			if (convertView == null){
				convertView = mInflater.inflate(R.layout.row_list_icon, null);
				
				holder = new ViewHolder();
				holder.title = (TextView) convertView.findViewById(R.id.titleRow);
				holder.icon = (ImageView) convertView.findViewById(R.id.listIcon);
				
				convertView.setTag(holder);
			}
			else {
				holder = (ViewHolder) convertView.getTag();
			}
			
			try {
				holder.title.setText(list.get(pos));
			} catch (Exception e) {}
			
			return convertView;
		}
	}
	
	static class ViewHolder {
		TextView title;
		ImageView icon;
	}
}
